// OnlineConnectivity.cs created with MonoDevelop
// User: ikex at 5:02 A 12/09/2008
//

using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Collections.Specialized;

namespace snippetmanager
{
	public class OnlineConnectivity
	{
		public string Username = "";
		private string Password = "";
		private WebClient wclient;
		public OnlineConnectivity(string username, string password)
		{
			Console.WriteLine("Online Conectivity class loaded with the username: "+username);
			this.Username = username;
			this.Password = password;
			this.wclient = new WebClient ();
		}
		/// <summary>
		/// Check if the users login is currently correct
		/// will return "OK" if it is "FAIL" if it isnt.
		/// </summary>
		/// <returns>
		/// A <see cref="System.String"/>
		/// </returns>
		public string LoginCheck() {
			try {
				string zdata = wclient.DownloadString("http://ikex.ath.cx:1010/work/snippetmanager/logincheck.php?user="+this.Username+"&pass="+this.Password);
				Console.WriteLine("[DEBUG web]: "+zdata);
				return zdata;
			}
			catch (Exception z) {
				Console.WriteLine("Web Error: "+z.Message);
				return "FAIL";
			}
		}
		/// <summary>
		/// Uploads a snippet to the users account.
		/// </summary>
		/// <param name="entry">
		/// A <see cref="Entry"/>
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/>
		/// </returns>
		public string UploadSnippet(Entry entry) {
			try {
				Console.WriteLine("Sending "+entry.Name+"..");
				NameValueCollection formData = new NameValueCollection(); 
				formData["user"] = this.Username; 
				formData["pass"] = this.Password; 
				formData["name"] = entry.Name; 
				formData["lang"] = entry.Lang;
				formData["code"] = entry.Code;
				byte[] bret = wclient.UploadValues("http://ikex.ath.cx:1010/work/snippetmanager/uploadsnippet.php", "POST", formData);
 
				string sret = System.Text.Encoding.ASCII.GetString(bret);
				Console.WriteLine("[DEBUG web]: "+sret);
				return sret;
			}
			catch (Exception z) {
				Console.WriteLine("Web Error: "+z.Message);
				return "FAIL";
			}
		}
		/// <summary>
		/// Incomplete!
		/// </summary>
		/// <param name="SnippetID">
		/// A <see cref="System.String"/>
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/>
		/// </returns>
		public string GetSnippet(string SnippetID) {
			//TODO: Write Me!
			return "FAIL";
		}
		
	}
}
